package ssf.app.action;

import java.io.*;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class DownloadAction extends BaseAction {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		byte[] b = new byte[1024];
		int i = 0;
		Map fileInfo = getServMgr().getFileService().getFileById(
				request.getParameter("fileId"));
		FileInputStream fis = new FileInputStream(getServlet()
				.getServletContext().getRealPath(
						fileInfo.get("filePath").toString()));
		response.setContentType("application/x-msdownload");
		response.setHeader("Content-Disposition", "Disposition; filename="
				+ fileInfo.get("fileName") + "." + fileInfo.get("extName"));
		OutputStream os = response.getOutputStream();
		while ((i = fis.read(b)) != -1) {
			os.write(b, 0, i);
			i = 0;
		}
		return null;
	}
}
